import { Injectable } from "@nestjs/common";
import { DeptRoleEnum, RoleEnum } from "./role.enum";
import routerlist, { basicRouter } from "./router";


@Injectable()
export class RoleService {
    constructor(

    ) { }
    public getRoleRouter(userRole: RoleEnum, deptRole: DeptRoleEnum) {
        // return routerlist.map(e => {
        //     return {
        //         ...e, children: e.children.filter(({ role }) => {
        //             if (!role) return true;
        //             if (userRole === RoleEnum.boos) return true;
        //             if (userRole === RoleEnum.director) {
        //                 return !role.some(e => e === RoleEnum.boos)
        //             }
        //             if (role.some((r) => r === userRole || r === deptRole)) return true;
        //             return false;
        //         })
        //     };
        // }).filter(e => !!e.children.length).map(e => {
        //     e.path = e.children[0].path;
        //     return e;
        // });
        const result = routerlist.filter(({ role }: any) => {
            if (!role) return true;
            if (userRole === RoleEnum.boos) return true;
            if (userRole === RoleEnum.director) {
                return !role.some(e => e === RoleEnum.boos)
            }
            if (role.some((r) => r === userRole || r === deptRole)) return true;
            return false;
        })
        if (userRole === RoleEnum.boos || userRole === RoleEnum.director) {
            return basicRouter.concat(this.order(result, userRole))
        } else {
            return basicRouter.concat(result);
        }
    }
    private order(list: any[], userRole: RoleEnum) {
        const names = ["人事部", "行政部", "商务部", "财务部",]
        const arr = [
            DeptRoleEnum.personnel,
            DeptRoleEnum.administration,
            DeptRoleEnum.business,
            DeptRoleEnum.finance,
        ]
        return list.filter(e => {
            if (!e.role) return false;
            if (e.role.length === 1) {
                if (e.role[0] === RoleEnum.manager) return true;
                if (e.role[0] === RoleEnum.director) return true;
                if (e.role[0] === RoleEnum.boos) return userRole === RoleEnum.boos
            }
            return false;
        }).concat(arr.map((role, index) => {
            return {
                name: names[index],
                children: list.filter(e => e.role?.some(a => a == role))
            };
        })).concat(list.filter(e => !e.role)) as any
    }
}